package com.wdz.utils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 封装了前端查询数据库的参数与需要查询的字段的封装
 */
public class ParamsUtil implements Serializable {

    private static final long serialVersionUID = 7251665211816225675L;

    private Map<String,Object> params;
    private String[] cloums;

    public Map<String, Object> getParams() {
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    public String[] getCloums() {
        return cloums;
    }

    public void setCloums(String[] cloums) {
        this.cloums = cloums;
    }

    public String getSqlWhere(){
        if(params == null){
            throw new RuntimeException("params不能为null");
        }
        Set<String> keys = params.keySet();
        StringBuffer sql_where = new StringBuffer();
        int index = 0;
        for(String key:keys){
            if(index != keys.size()-1) {
                sql_where.append(key + "={" + index + "} and ");
            }else{
                sql_where.append(key + "={" + index + "}");
            }
            index ++;
        }
        System.out.println("sql打印："+sql_where);
        return sql_where.toString();
    }

    public Object[] getSqlValues(){
        if(params == null){
            throw new RuntimeException("params不能为null");
        }
        Set<String> keys = params.keySet();
        List<Object> values = new ArrayList<Object>();
        for(String key:keys){
            values.add(params.get(key));
        }
        return values.toArray();
    }
}
